﻿using System.Security.Claims;
using System.Threading.Tasks;
using K9Nano.LayIM.Models;
using K9Nano.LayIM.Stores;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace K9Nano.LayIM.Controllers
{
    [Area("IM")]
    [ApiController]
    [Route("api/im/chat")]
    public class ChatController: ControllerBase
    {
        private readonly IChatFriendStore _friendStore;
        private readonly LayIMOptions _options;

        public ChatController(IChatFriendStore friendStore, IOptions<LayIMOptions> options)
        {
            _friendStore = friendStore;
            _options = options.Value;
        }

        [Authorize]
        [HttpGet("init")]
        public async Task<LayIMApiResponse<LayIMInitResponseData>> Init()
        {
            var userId = User.FindFirstValue(_options.UserIdentifierClaimType);

            var mine = await _friendStore.GetUserAsync(userId, HttpContext.RequestAborted);

            var friend = await _friendStore.GetFriendsAsync(userId, HttpContext.RequestAborted);

            var group = await _friendStore.GetGroupsAsync(userId, HttpContext.RequestAborted);
            
            return new LayIMApiResponse<LayIMInitResponseData>(new LayIMInitResponseData
            {
                mine = mine,
                friend = friend,
                group = group
            });
        }
    }
}